package com.watson.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.watson.R;
import com.watson.utils.StringUtils;

import java.util.ArrayList;
import java.util.Map;


public class DetailsAdapter extends RecyclerView.Adapter<DetailsAdapter.ItemViewHolder> {
    private ItemClickListener mItemClick;
    private Context mContext;
    private ArrayList<Map> mList;
    private int code = 0;

    public DetailsAdapter(Context mContext, ArrayList<Map> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    public void setData(ArrayList<Map> vList) {
        this.mList = vList;
        notifyDataSetChanged();
    }

    public void setCode(int code) {
        this.code = code;
        notifyDataSetChanged();
    }

    public void setItemClickListener(ItemClickListener listener) {
        this.mItemClick = listener;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemViewHolder itemViewHolder = new ItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.itme_details, parent, false));
        return itemViewHolder;
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, final int position) {
        if (mList.size() > 0) {
            holder.tvname.setText(mList.get(position).get("name") + " " + mList.get(position).get("erpno"));
            holder.tvsly.setText(StringUtils.isEmpty(mList.get(position).get("num") + "") ? "0" : mList.get(position).get("num") + "");
            holder.tvPin.setText(StringUtils.isEmpty(mList.get(position).get("brand") + "") ? "" : mList.get(position).get("brand") + "");
            holder.tvstd.setText(StringUtils.isEmpty(mList.get(position).get("std") + "") ? "" : mList.get(position).get("std") + "");
            ArrayList<Map> codesupplierList = new ArrayList<>();
            if (mList.get(position).containsKey("codesupplierList")) {
                codesupplierList = (ArrayList<Map>) JSON.parseArray(mList.get(position).get("codesupplierList") + "", Map.class);
                String code = "";
                String car = "";
                if (codesupplierList.size() > 0) {
                    for (int i = 0; i < codesupplierList.size(); i++) {
                        if (i == 0) {
                            code = codesupplierList.get(i).get("code") + "";
                        } else {
                            code = code + "/" + codesupplierList.get(i).get("code") + "";
                        }
                        if (i == 0) {
                            car = codesupplierList.get(i).get("car") + "";
                        } else {
                            car = car + "/" + codesupplierList.get(i).get("car") + "";
                        }
                    }
                }
                holder.tverpno.setText(code);
                holder.tvcar.setText(car);
            }

            holder.tvout_storage.setText(code + "");

            holder.itemView.setOnClickListener(v -> {
                if (mItemClick != null) {
                    mItemClick.onClick(position, mList);
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        private TextView tvstd, tverpno, tvkf, tvsly, tvout_storage, tvname, tvcar, tvPin;

        public ItemViewHolder(View itemView) {
            super(itemView);
            tvstd = itemView.findViewById(R.id.tvstd);
            tverpno = itemView.findViewById(R.id.tverpno);
            tvkf = itemView.findViewById(R.id.tvkf);
            tvsly = itemView.findViewById(R.id.tvsly);
            tvout_storage = itemView.findViewById(R.id.tvout_storage);
            tvname = itemView.findViewById(R.id.tvname);
            tvcar = itemView.findViewById(R.id.tvcar);
            tvPin = itemView.findViewById(R.id.tvPin);
        }
    }

    public interface ItemClickListener {
        void onClick(int position, ArrayList<Map> mList);
    }
}
